package com.training.food.controller;

import com.alibaba.fastjson.JSON;
import com.training.food.Mapper.SlaughterMapper;
import com.training.food.pojo.Check;
import com.training.food.pojo.Slaughter;
import com.training.food.pojo.User;
import com.training.food.service.CheckService;
import com.training.food.service.SlaughterService;
import com.training.food.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Map;
@RestController
public class SlaughterController {
    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Resource
    SlaughterService slaughterService;
    @Resource
    CheckService checkService;
    @Autowired
    SlaughterMapper slaughterMapper;
    //增加屠宰场信息
    @PostMapping("/food/buy/addSlaughter")
    public CommonResult Addslaughter(HttpServletRequest request, @RequestBody Slaughter slaughter) throws UnsupportedEncodingException {
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        slaughter.setUsername(user.getUsername());
        slaughterService.add(slaughter);
        // System.out.println(request.getHeader("token"));
        return CommonResult.success("添加成功");
    }
    //根据权限返回所有订单
    //根据state返回
    //查询屠宰场所有信息
    @PostMapping("/food/buy/selectAllSlaughter")
    public CommonResult selectAllSlaughter(HttpServletRequest request, @RequestBody Slaughter slaughter) throws UnsupportedEncodingException{

        List<Slaughter> lists=slaughterService.selectAll(slaughter);
        if (lists.size()>0){
            return CommonResult.success(lists);
        }
        return CommonResult.failed("没有找到");
    }

    @PostMapping("/food/buy/selectSlaughter")
    //查询屠宰场信息
    public CommonResult selectSlaughter(HttpServletRequest request, @RequestBody Slaughter slaughter) throws UnsupportedEncodingException{

        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        if(user==null){
            CommonResult.failed("用户没有找到");
        }
        List<Slaughter> lists=slaughterService.selectByUser(user.getUsername(),slaughter.getState());
        if (lists.size()>0){
            return CommonResult.success(lists);
        }
        return CommonResult.failed("没有找到");
    }
    //更改屠宰场交易信息状态
    @PostMapping("/food/buy/updateSlaughter")
    public CommonResult Updateslaughter(HttpServletRequest request, @RequestBody Slaughter slaughter) throws UnsupportedEncodingException{
        String token= (String) request.getAttribute("user");
        Object obj=redisTemplate.opsForValue().get(token);
        User user=(User)obj;
        slaughter.setUsername(user.getUsername());
        Slaughter slaughter1=slaughterMapper.select(slaughter);
        slaughter1.setState(slaughter.getState());
        slaughterService.update(slaughter1);
        return CommonResult.success("修改成功");
    }
    //
    //删除屠宰场信息
    @PostMapping("/food/buy/deleteSlaughter")
    public CommonResult Deleteslaughter(HttpServletRequest request, @RequestBody Map<String,Object> req)throws UnsupportedEncodingException {

        Map<String,String>sl= (Map<String, String>) req.get("slaughter");
        Map<String,String>ch= (Map<String, String>) req.get("check");
        Slaughter slaughter= JSON.parseObject(JSON.toJSONString(sl), Slaughter.class);
        Check check=JSON.parseObject(JSON.toJSONString(ch), Check.class);
        if(slaughterService.select(slaughter)){
            slaughterService.delete(slaughter);
            if(checkService.select(check)){
                checkService.sub(check);
            }
            return CommonResult.success("删除成功");
        }

        return CommonResult.failed("商品不存在");
    }

}
